package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.comenjoysoft.baselibrary.util.ThreadUtils;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.base.Config;
import com.yuanli.cosmeticmirror.utils.FileUtils;
import com.yuanli.cosmeticmirror.view.adapter.PhotoAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的照片界面
 */
public class PhotoActivity extends BaseActivity {
    @BindView(R.id.photo_back)
    ImageView photoBack;
    @BindView(R.id.photo_rv)
    RecyclerView photoRv;

    private PhotoAdapter adapter;
    private int requestCode = 101;

    @Override
    protected int getContentView() {
        return R.layout.activity_photo;
    }

    @Override
    protected void init(Bundle bundle) {

    }

    @Override
    protected void initView() {
        adapter = new PhotoAdapter();
        photoRv.setLayoutManager(new GridLayoutManager(this, 3));
        photoRv.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, position) -> {
            String s = (String) adapter.getItem(position);
            if (!TextUtils.isEmpty(s)) {
                startActivityForResult(new Intent(PhotoActivity.this, PhotoInfoActivity.class).putExtra("imgUrl", s), requestCode);
            }
        });
    }

    @Override
    protected void initData() {
        showLoadDialog();
        ThreadUtils.executeByIo(new ThreadUtils.Task<List<String>>() {
            @Override
            public List<String> doInBackground() throws Throwable {
                List<File> allFileToDir = FileUtils.getAllFileToDir(FileUtils.getSDCardRoot() + Config.PHOTO_PATH);
                List<String> strings = new ArrayList<>();
                for (File file : allFileToDir) {
                    if (file.exists()) {
                        strings.add(file.getAbsolutePath());
                    }
                }
                return strings;

            }

            @Override
            public void onSuccess(List<String> result) {

                dialogDismiss();
                adapter.setNewInstance(result);
            }

            @Override
            public void onCancel() {
                dialogDismiss();
            }

            @Override
            public void onFail(Throwable t) {
                dialogDismiss();
            }
        });

    }

    @Override
    protected void destroy() {

    }


    @OnClick(R.id.photo_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
                if (this.requestCode == requestCode) {
                    String delete = data.getStringExtra("delete");
                    if (!TextUtils.isEmpty(delete)) {
                        adapter.remove(delete);
                    }

                }
            }
        }
    }
}
